package com.hqj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqj.entity.TRole;
import com.hqj.entity.TRoleMenu;
import com.hqj.service.TRoleMenuServiceImpl;
import com.hqj.service.TRoleServiceImpl;
import com.hqj.util.ReturnValueUtil;
import net.sf.jsqlparser.statement.create.table.Index;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hqj
 * @since 2020-06-11
 */
@Controller
@RequestMapping("/roc")
public class TRoleController extends  BaseController<TRole> {

    @Resource(name="TRoleServiceImpl")
    private TRoleServiceImpl tsi;

    @Resource(name="TRoleMenuServiceImpl")
    private TRoleMenuServiceImpl rmsi;

    @Override
    public String index() {
        return "admin/role/index";
    }

    @Override
    public String find(TRole tRole) {
        List<TRole> list = tsi.list(null);
        return ReturnValueUtil.findData(list.size(),list);
    }

    @RequestMapping("/find2")
    @ResponseBody
    public List<TRole> find2(@Param("uid") int uid){
        List<TRole> list = tsi.findUserRoleMenu(uid);
        return list;
    }

    @RequestMapping("/grant")
    @ResponseBody
    public Map<String,Object> grant(@Param("rid") int rid,@Param("mids") String mids){
        Map<String,Object> map = new HashMap<>();
        //先执行删除
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid",rid);
        boolean b = rmsi.remove(queryWrapper);
        //在执行批量插入
        String ms[] = mids.split(",");
        if(ms.length>0){
            List<TRoleMenu> list = new ArrayList<>();
            for(String mid : ms) {
                if(mid=="") continue;
                TRoleMenu rm = new TRoleMenu();
                rm.setRid(rid);
                rm.setMid(Integer.parseInt(mid));
                list.add(rm);
            }
            b = rmsi.saveBatch(list);
        }
        return ReturnValueUtil.aer(b,"授权");
    }
}
